import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class dePluralizer {

	/**
	 * @param args
	 */
	
	
	public String dePluralizeDevanagari(String word, Map<String, String> words){		
		for(int i = 0; i < word.length(); i++){
			String w = "";
			if(word.substring(i).equals("ाः")){
				w = word.substring(0, i) + "ः";
				if(words.containsKey(w)){
					return w;
				}//end if
			}//end if
			else if(word.substring(i).equals("ा") && i == word.length()-1){
				w = word.substring(0, i) + "ः";
				if(words.containsKey(w)){
					return w;
				}//end if
			}//end if
			else if(word.substring(i).equals("ानि")){
				w = word.substring(0, i) + "म्";
				if(words.containsKey(w)){
					return w;
				}//end if
			}//end else if
			else if(word.substring(i).equals("वः")){
				w = word.substring(0, i) + "ुः";
				if(words.containsKey(w)){
					return w;
				}//end if
			}//end else if
		}//end for
		return null;
	}//end method
	
	public String dePluralize(String word, Map<String, String> words){
		String a = dePluralizeDevanagari(word, words);
		if (a != null)
			return a;
		for(int i = 0; i < word.length() + 1; i++){
			String w = "";
			if(word.substring(i).equals("aaH")){
				w = word.substring(0, i + 1) + "H";
				String w2 = word.substring(0, i+2);
				if(words.containsKey(w)){
					return w;
				}//end if
				else if(words.containsKey(w2)){
					return w2;
				}
			}//end if
			else if(word.substring(i).equals("aaNi") || word.substring(i).equals("aani")){
				w = word.substring(0, i+1) + "M";
				if(words.containsKey(w)){
					return w;
				}//end if
			}//end else if
			else if(word.substring(i).equals("avaH")){
				w = word.substring(0, i) + "uH";
				if(words.containsKey(w)){
					return w;
				}//end if
			}//end else if
		}//end for
		return "Word is already singular.";
	}//end method
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String word;
		Map <String, String> words = new HashMap<String, String>();
		Scanner sc1;
		try {
			sc1 = new Scanner(new File("dictionaryDePluralizer.txt"));
			while(sc1.hasNext()){
				String s = sc1.nextLine();
				words.put(s, "1");
			}//end while
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//end catch
		System.out.println(words);
		System.out.println(words.containsKey("गजः"));
		while (true) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word: ");
		word = sc.nextLine();
		if (word.equals("q"))
			break;
		dePluralizer d = new dePluralizer();
		System.out.println(d.dePluralize(word, words));
	}//end method
	}
}//end class
